<template>
  <div id="activityEchartsTop">
    <div class="search-box">
      <el-form
        ref="ruleForm"
        :inline="true"
        :model="formInline"
        :rules="rules"
        class="demo-form-inline"
      >
        <el-form-item label="统计时间" prop="createTime">
          <el-date-picker
            v-model="formInline.createTime"
            type="daterange"
            range-separator="至"
            start-placeholder="开始时间"
            end-placeholder="结束时间"
            value-format="yyyy-MM-dd"
            :picker-options="pickerOptions"
          >
          </el-date-picker>
        </el-form-item>
        <el-form-item class="s-btn">
          <el-button
            type="success"
            icon="el-icon-search"
            @click="toSearch('ruleForm')"
          >
            查询
          </el-button>
          <el-button type="success" plain @click="reset">
            重置
          </el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
import { sysNormalDisable } from '@utils/dictionariesFunc'
import { dateRangeMixins } from '@/mixins/dateRange'
export default {
  name: 'activityEchartsTop',
  msg: '活跃分析-顶部搜索',
  mixins: [dateRangeMixins],
  props: {
    formInline: {
      type: Object,
      default: function() {
        return {
          // 搜索条件
          startDate: '', // 开始时间
          endDate: '', // 结束时间
          createTime: []
        }
      }
    }
  },
  data() {
    return {
      rules: {
        createTime: {
          type: 'array',
          required: true,
          message: '统计时间不能为空'
        }
      }
    }
  },
  watch: {
    'formInline.createTime': {
      handler(newVal, oldVal) {
        if (newVal && newVal.length > 1) {
          this.formInline.startDate = newVal[0]
          this.formInline.endDate = newVal[1]
        } else {
          this.formInline.startDate = ''
          this.formInline.endDate = ''
        }
      },
      deep: true
    }
  },
  mounted() {
    this.getStatusList()
  },
  methods: {
    /* 获取字典状态 */
    async getStatusList() {
      let periodList = await sysNormalDisable('activity_analysis_type')
      this.getCreateDate('Date')
      this.$emit('setTypeList', periodList)
    },
    /* 搜索 */
    toSearch(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.$emit('toSearch')
        } else {
          return false
        }
      })
    },
    /* 重置 */
    reset() {
      this.$refs['ruleForm'].resetFields()
      this.getCreateDate('Date')
    }
  }
}
</script>

<style lang="less"></style>
